<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
<jsp:include page="/pages/sysIndex/default.jsp"/>
<script src="<c:url value="/resources/base/js/bootstrap-treeview.js"/>"></script>
<div class="main-content">
    <div class="box box-main">
        <div class="box-header">
            <div class="box-title">
                <%--<i class="fa icon-trophy"></i> 用户管理--%>
                <sec:authorize access="hasAnyRole({'ROLE_ADMIN_ORGANIZATION_EDIT'})">
                    <a href="javascript:;" onclick="vLayuiUtil.openLayerMaxWindow('vOrganization', '<c:url
                            value="/web/admin/organization/organizeTreePage.action"/>','编辑组织架构')"
                       class="btn btn-default btnTool"
                       title="编辑组织架构"><i class="fa fa-plus"></i> 编辑组织架构</a>
                </sec:authorize>
            </div>
            <div class="box-tools pull-right">
                <%--  <a href="#" class="btn btn-default" id="btnSearch" title="查询"><i class="fa fa-filter"></i> 查询</a>--%>
                <%-- <a href="javascript:;" onclick="vLayuiUtil.openLayerMaxWindow('vOrganization', '<c:url
                         value="/web/admin/organization/organizeTreePage.action"/>','编辑组织架构')" class="btn btn-default btnTool"
                    title="编辑组织架构"><i class="fa fa-plus"></i> 编辑组织架构</a>--%>
                <sec:authorize
                        access="hasAnyRole({'ROLE_ADMIN_PERSONNEL_EDIT'})">
                    <a href="javascript:;" onclick="vLayuiUtil.openLayerMaxWindow('wPersonnelImport', '<c:url
                            value="/web/admin/personnel/importPage.action"/>','批量导入')" class="btn btn-default btnTool"
                       title="新增用户"><i class="fa fa-plus"></i> 批量导入</a>
                    <a href="javascript:;" onclick="vLayuiUtil.openLayerMaxWindow('wPersonnel', '<c:url
                            value="/web/admin/personnel/editPage.action"/>','新增')" class="btn btn-default btnTool"
                       title="新增用户"><i class="fa fa-plus"></i> 新增</a>
                </sec:authorize>

            </div>
        </div>


        <div class="box-body">
            <form id="searchForm" action="<c:url value="/web/admin/personnel/listJson.action"/>" method="post"
                  class="form-inline hide" data-page-no="1" data-page-size="20" data-order-by="">
                <div class="form-group">
                    <label class="control-label">搜索：</label>
                    <div class="control-inline">
                        <input type="text" id="name" name="name" value="" placeholder="用户名称"
                               maxlength="64"
                               class="form-control width-120">
                        <input type="hidden" id="organizationId" name="organizationId" value="" placeholder="部门ID"
                               maxlength="64"
                               class="form-control width-120">
                    </div>
                </div>

                <div class="form-group">
                    <button type="submit" class="btn btn-primary btn-sm">查询</button>
                    <button type="reset" class="btn btn-default btn-sm">重置</button>
                </div>
            </form>

            <div id="tree" class="div-inline" style="width: 15%;"></div>
            <div id="table" class="div-inline" style="width: 80%;margin-left: 2%;">
                <div style="overflow-x:auto">
                    <table id="dataGrid"></table>
                </div>
                <div id="dataGridPage"></div>
            </div>
        </div>
    </div>
</div>
<style>
    .div-inline {
        float: left
    }
</style>
<script>
    $(function () {
        $.ajax({
            type: "Post",
            url: "<c:url value="/web/admin/organization/treeData.action"/>",
            dataType: "json",
            success: function (result) {
                $('#tree').treeview({
                    data: result.data,         // 数据源
                    showCheckbox: false,   //是否显示复选框
                    highlightSelected: true,    //是否高亮选中
                    //nodeIcon: 'glyphicon glyphicon-user',    //节点上的图标
                    nodeIcon: ''/*'glyphicon glyphicon-globe'*/,
                    emptyIcon: '',    //没有子节点的节点图标
                    multiSelect: false,    //多选
                    levels: 2,
                    onNodeChecked: function (event, data) {
                        console.log(data);
                    },
                    onNodeSelected: function (event, data) {
                        $("#organizationId").val(data.id);
                        $('#dataGrid').dataGrid('refresh');
                        //  alert(data);
                    }
                });
            },
            error: function () {
                alert("树形结构加载失败！")
            }
        });
    })


    $('#dataGrid').dataGrid({
        shrinkToFit:false,
        autoScroll: true,
        // searchForm: $("#searchForm"),
        queryParams: {name: $("#name").val(), organizationId: $("#organizationId").val()},
        columnModel: [
            <sec:authorize  access="hasAnyRole({'ROLE_ADMIN_PERSONNEL_EDIT'})">

            {
                header: '操作',
                name: 'actions',
                width: 240,
                sortable: false,
                title: false,
                formatter: function (val, obj, row, act) {
                    if (!row.leader) {
                        return '<a  class="btn btn-default btn-xs btnList" href="<c:url value="/web/admin/personnel/designLeader.action?flag=1&userId="/>' + row.id + '"  title="设为负责人" data-confirm="确认要设为负责人吗？"><i class="fa fa-check-circle-o"></i>设为负责人</a>&nbsp;' +
                            '<a  class="btn btn-default btn-xs btnList" onclick="editPersonnel(' + row.id + ')" title="编辑人员"><i class="fa fa-pencil"></i>编辑人员</a>&nbsp;'
                                                    + '<a  class="btn btn-default btn-xs btnList" href="<c:url value="/web/admin/personnel/delete.action?&userId="/>' + row.id + '"  title="删除" data-confirm="确认离职？"><i class="fa fa-times"></i>离职</a>&nbsp;'
                    } else {
                        return '<a  class="btn btn-default btn-xs btnList" href="<c:url value="/web/admin/personnel/designLeader.action?flag=0&userId="/>' + row.id + '"  title="取消负责人" data-confirm="确认要取消负责人身份吗？"><i class="fa fa-reply-all"></i>取消负责人</a>&nbsp;' +
                            '<a  class="btn btn-default btn-xs btnList" onclick="editPersonnel(' + row.id + ')" title="编辑人员"><i class="fa fa-pencil"></i>编辑人员</a>&nbsp;'
                                                    + '<a  class="btn btn-default btn-xs btnList" href="<c:url value="/web/admin/personnel/delete.action?&userId="/>' + row.id + '"  title="删除" data-confirm="确认离职？"><i class="fa fa-times"></i>离职</a>&nbsp;'
                    }
                }
            },
            </sec:authorize>
            {header: '编码', name: 'id', index: 'id', width: 50,sortable: false, align: "center"},
            {
                header: '姓名',
                name: 'name',
                index: 'name',
                width: 180,
                align: "center",
                sortable: false,
                formatter: function (val, obj, row, act) {
                    if (row.leader) {
                        return '<a  class="btn btn-default btn-xs btnList" >负责人</a>' + row.name
                    }
                    return row.name;
                }
            },
            {header: '性别', name: 'sex', index: 'sex', width: 100,sortable: false, align: "center"},
            {header: '手机号', name: 'mobile', index: 'mobile', width: 150,sortable: false, align: "center"},
            {header: '工号', name: 'employeeCode', index: 'employeeCode', width: 150,sortable: false, align: "center"},
            {header: '邮箱', name: 'email', index: 'email', width: 150,sortable: false,align: "center"},
            {header: '岗位', name: 'position', index: 'position', width: 150,sortable: false, align: "center"},
            {header: '部门', name: 'organization', index: 'organization', width: 150,sortable: false, align: "center"}
        ],
        // 加载成功后执行事件
        ajaxSuccess: function (data) {

        }
    });

    function editPersonnel(id) {
        vLayuiUtil.openLayerMaxWindow('wPersonnel', '<c:url
                        value="/web/admin/personnel/editPage.action?id="/>' + id, '编辑')
    }

</script>